
#ifdef __NDS__

#include "NintendoDS.h"
#include <netinet/in.h>

void connexioNDS(void){
	struct in_addr ip, gateway, mask, dns1, dns2;
	int done = 0;
	
	while(!done){
		fprintf(stderr,"Connecting...\n");
		if(!Wifi_InitDefault(WFC_CONNECT)) {
			fprintf(stderr,"Failed to connect!\n");
			done=0;
		} else {
			printf("Connected\n\n");
			ip = Wifi_GetIPInfo(&gateway, &mask, &dns1, &dns2);
			fprintf(stderr,"Your IP address: %s\n", inet_ntoa(ip) );
			done=1;
		}
	}
}




/*
void connexioNDS(void){
	struct in_addr ip, gateway, mask, dns1, dns2;
	printf("Connect? A=yes, B=no\n");
	int held;
	int done = 0;
	while(!done){
		scanKeys(); 
		held = keysHeld();
		if (held & KEY_A) { 
			while (held&KEY_A) {scanKeys(); held = keysHeld();}
			fprintf(stderr,"Connecting...\n");
			if(!Wifi_InitDefault(WFC_CONNECT)) {
				printf("Failed to connect!");
			} else {
				printf("Connected\n\n");
			}
			ip = Wifi_GetIPInfo(&gateway, &mask, &dns1, &dns2);
			fprintf(stderr,"Your IP address: %s\n", inet_ntoa(ip) );
			done=1;
		}
		if (held & KEY_B) { 
			while (held&KEY_A) {scanKeys(); held = keysHeld();}
			printf("Not connected!\n");
			done=1;
		}
	}
}
*/



int GetPortNDS(SDL_Surface * screen, SDLFont * font, int server) {
	int done=0;
	long old=0,port=0;
	char str[5];
	InitBackground(screen);
	if (!server) drawString(screen,font,15,10,"Enter Your Port (0 for random):");
	else drawString(screen,font,60,10,"Enter Your Port:");
	SDL_Flip(screen);
	keyboardDemoInit();
	keyboardShow();
	int key;
	while (!done) {
		key = keyboardGetChar();

		if ( key>0) {
			old=port;
			if (key == SDLK_ESCAPE) { return -1; }
			else if (key == 10) {
				if (!server) done=1; 
				else if (port) done=1;
				else {
					InitBackground(screen);
					drawString(screen,font,60,10,"Not 0 dumbass!");
					SDL_Flip(screen);
					SDL_Delay(200);
				}
			}
			else if (key <= 57 && key >= 48)
				port = 10*port + key - 48;
			else if (key == SDLK_BACKSPACE) { port = port / 10; }
			if (port > 65535) port = 65535;
			if (old!=port) {
				InitBackground(screen);
				(void) sprintf(str,"%ld",port);
				if (!server) drawString(screen,font,60,10,"Enter Your Port (0 for random):");
				else drawString(screen,font,60,10,"Enter Your Port:");
				drawString(screen,font,90,60,str);
				SDL_Flip(screen);
			}
		}
	}
	fprintf(stderr,"Your port: %d\n",port);
	keyboardHide();
	return port;
}





IPaddress GetAddressNDS(SDL_Surface * screen, SDLFont * font) {
	IPaddress ip; 
	SDLNet_ResolveHost(&ip, NULL, 1111);
	int done=0,i;
	int len=0,old;
	char str[101];
	char * host;
	long port=0;
	InitBackground(screen);
	drawString(screen,font,60,10,"Enter Host Address:Port");
	SDL_Flip(screen);

	keyboardDemoInit();
	keyboardShow();
	int key;

	for (i=0;i<101;i++)
		str[i]=0;
	while (!done) {
		key = keyboardGetChar();

			if (key>0) {
				old = len;
				if (key == 10) { done=1; }
				else if (key == 107) { str[len]=58; //letra "k" es ":"
									len++;
					 }
				else if ((key <= 57 && key >= 48) || 
						key == 46 ||
						(key <= 90 && key >= 65) ||
						(key <= 122 && key >= 97)) {
					str[len]=key;
					len++;
				}

				else if (key == SDLK_SEMICOLON ) {
					str[len]=58;
					len++;
				} // el ";" tambien es ":"

				else if (key == SDLK_BACKSPACE) { 
					len--; 
					str[len]=0;
				}
				if (len>=100) len=100;
				if (old!=len) {
					InitBackground(screen);
					drawString(screen,font,60,10,"Enter Host Address:Port");
					drawString(screen,font,90,60,str);
					SDL_Flip(screen);
				}
			}
	}

	keyboardHide();

	i=0;
	while (str[i] != 58)
		i++;
	len=i;

	host = (char *) calloc(i+1,sizeof(char));

	for (i=0;i<len;i++)
		host[i]=str[i];
	i++;
	while (str[i] <= 57 && str[i] >= 48) {
		port = 10*port 	+ str[i]-48;
		i++;
	} 
	if (port > 65535) port = 65535;

	if (!SDLNet_ResolveHost(&ip, host, port)) return ip;
	else {
		SDLNet_ResolveHost(&ip, NULL, 1111);
		return ip;		
	}
}

#endif
